package model

import (
	"gopkg.in/mgo.v2"
	"labix.org/v2/mgo/bson"
)

type Message struct {
	HoldUserId   int   `gorm:"type:int;not null;" json:"hold_user_id"  bson:"hold_user_id"` //发送者id
	ObjectUserId int   `gorm:"type:int;not null;" json:"object_id"  bson:"object_user_id"`  // 聊天对象id
	IsDelete     int64 `gorm:"type:int;not null;" json:"is_delete"  bson:"is_delete"`       //  1删除
}

//新增聊天列表
func (message *Message) MessageInsert(mgo *mgo.Collection) error {
	mgo.Insert(&message)
	from := message.HoldUserId
	to := message.ObjectUserId
	message.HoldUserId = to
	message.ObjectUserId = from
	err := mgo.Insert(&message)
	return err
}

//获取聊天列表
func (message *Message) SelectMessageUserIdList(mgo *mgo.Collection) ([]Message, error) {
	var messageList []Message
	err := mgo.Find(bson.M{"hold_user_id": message.HoldUserId}).All(&messageList)
	return messageList, err
}

//根据双方id判断发送者的聊天列表里是否有对方
func (message *Message) IsMessageUserIdAndObjectUserId(mgo *mgo.Collection) int {
	amount, _ := mgo.Find(bson.M{"hold_user_id": message.HoldUserId, "object_user_id": message.ObjectUserId, "is_delete": 0}).Count()
	return amount
}
